package com.zivwu.mvp.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.DialogFragment;

import com.zivwu.mvp.view.IView;
import com.zivwu.mvp.view.fragment.IFragment;
import com.zivwu.utils.device.FragmentHelper;
import com.zivwu.utils.device.ToastHelper;

import dagger.android.support.DaggerAppCompatActivity;


public abstract class IActivity extends DaggerAppCompatActivity
        implements IView {

    private DialogFragment mProgress;

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public AppCompatActivity getActivity() {
        return this;
    }


    @Override
    public void showDialog(DialogFragment fragment) {
        FragmentHelper.showDialog(fragment, getSupportFragmentManager());
    }

    protected void replaceFragment(IFragment fragment,
                                   Pair<View, String>... sharedElements) {
        replaceFragment(fragment, false, sharedElements);
    }

    protected void replaceFragment(IFragment fragment,
                                   boolean addToBackStack,
                                   Pair<View, String>... sharedElements) {

        FragmentHelper.replace(fragment.getRes(),
                fragment,
                getSupportFragmentManager(),
                addToBackStack,
                sharedElements);
    }

    /* ************************same*****************************/


    @Override
    public Bundle getTransitionBundle() {
        return ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle();
    }


    @Override
    public void showToast(String msg) {
        ToastHelper.showMsg(msg);
    }

    @Override
    public void showToast(int id) {
        showToast(getString(id));
    }

    @Override
    public void onLoadStart() {
        if (mProgress == null)
            mProgress = getLoadingDialog();
        showDialog(mProgress);
    }

    protected abstract DialogFragment getLoadingDialog();

    @Override
    public void onLoadFinish() {
        if (mProgress != null)
            mProgress.dismissAllowingStateLoss();
    }


}
